package Solution.problem057;

import com.sun.tools.javac.util.StringUtils;

/**
 * @program Leetcode
 * @description:
 * @author: lishangsheng
 * @create: 2019/05/30 16:26
 */
public class Solution {
    public static int lengthOfLastWord(String s) {
        if (s == null || s.equals("")) {
            return 0;
        }
        s=s.trim();
        /*记录字单词开始结束位置*/
        int startId = 0;
        int endId = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                startId = i;
                endId = i;
            } else {
                endId++;
            }
        }
        return endId - startId;
    }

    public static void main(String[] args) {
        String str = "Hello World";
        System.out.println(lengthOfLastWord(str));

        String str2 = "     ";
        System.out.println(lengthOfLastWord(str2));

        String str3 = "a     ";
        System.out.println(lengthOfLastWord(str3));

        String str4 = "a";
        System.out.println(lengthOfLastWord(str4));

        String str5 = "Today is a nice day";
        System.out.println(lengthOfLastWord(str5));
    }
}
